package com.yzt.message.custom;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.yzt.zhuju.IService.call.CallConstants;
import com.yzt.zhuju.RXingUtils;
import com.yzt.message.R;

import java.util.List;

import io.rong.imkit.conversation.extension.component.emoticon.AndroidEmoji;
import io.rong.imkit.model.UiMessage;
import io.rong.imkit.widget.adapter.IViewProviderListener;
import io.rong.imkit.widget.adapter.ViewHolder;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.MessageContent;

/**
 * 功能：声网语音/视频展示的布局
 */
public class RxAgoraCallMessageProvider extends CustomBaseMessageItemProvider<RxAgoraCallMessageContent> {

    public RxAgoraCallMessageProvider() {
        mConfig.showReadState = true;
        mConfig.showContentBubble = false;
    }

    @Override
    protected ViewHolder onCreateMessageContentViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_nomi_call_message_item_provider, parent, false);
        return new ViewHolder(view.getContext(), view);
    }

    @Override
    protected void bindMessageContentViewHolder(ViewHolder holder, io.rong.imkit.widget.adapter.ViewHolder parentHolder, RxAgoraCallMessageContent content, UiMessage message, int position, List<UiMessage> list, IViewProviderListener<UiMessage> iViewProviderListener) {
        LinearLayout llContent = holder.getView(R.id.ll_content);
        TextView contentTv = holder.getView(R.id.tv_content);
        ImageView callTypeIv = holder.getView(R.id.call_type_iv);
        ImageView callTypeIv2 = holder.getView(R.id.call_type_iv2);

        if (message.getMessageDirection() == Message.MessageDirection.SEND) {
            llContent.setBackgroundResource(R.drawable.nomi_rc_ic_bubble_right);
            callTypeIv.setVisibility(View.GONE);
            callTypeIv2.setVisibility(View.VISIBLE);
            //1 语音类型  3视频类型
            callTypeIv2.setImageResource(content.getType().equals("3") ? R.mipmap.icon_message_call_video_self : R.mipmap.icon_message_call_voice_self);
            contentTv.setText(content.getSendContent());
            contentTv.setTextColor(ContextCompat.getColor(contentTv.getContext(), R.color.color_FFFFFF));
        } else {
            llContent.setBackgroundResource(R.drawable.nomi_rc_ic_bubble_left);
            callTypeIv.setVisibility(View.VISIBLE);
            callTypeIv2.setVisibility(View.GONE);
            //1 语音类型  3视频类型
            callTypeIv.setImageResource(content.getType().equals("3") ? R.mipmap.icon_message_call_video : R.mipmap.icon_message_call_voice);

            contentTv.setText(content.getReceiveContent());
            contentTv.setTextColor(ContextCompat.getColor(contentTv.getContext(), R.color.color_262628));
        }
    }

    @Override
    protected boolean onItemClick(io.rong.imkit.widget.adapter.ViewHolder viewHolder, RxAgoraCallMessageContent rxAgoraCallMessageContent, UiMessage uiMessage, int i, List<UiMessage> list, IViewProviderListener<UiMessage> iViewProviderListener) {

        String targetId;
        if (uiMessage.getMessageDirection() == Message.MessageDirection.SEND) {
            targetId = uiMessage.getTargetId();
        } else {
            targetId = uiMessage.getSenderUserId();
        }

        int type = CallConstants.TYPE_VOICE;
        if ("3".equals(rxAgoraCallMessageContent.getType())) {
            //视频
            type = CallConstants.TYPE_VIDEO;
        }

      //  ARouter.getInstance().navigation(ICallService.class).replayCall(type, targetId);
        return false;
    }

    @Override
    protected boolean isMessageViewType(MessageContent messageContent) {
        return messageContent instanceof RxAgoraCallMessageContent && !messageContent.isDestruct();
    }

    @Override
    public Spannable getSummarySpannable(Context context, RxAgoraCallMessageContent rxAgoraCallMessageContent) {
        if (rxAgoraCallMessageContent == null) {
            return null;
        }
        String content;
        if (!TextUtils.equals(rxAgoraCallMessageContent.getSenderId(), RXingUtils.getUserId())) {
            content = rxAgoraCallMessageContent.getReceiveContent();
        } else {
            content = rxAgoraCallMessageContent.getSendContent();
        }
        if (rxAgoraCallMessageContent.isDestruct()) {
            return new SpannableString(content);
        }
        if (content != null) {
            if (content.length() > 100) {
                content = content.substring(0, 100);
            }
            return new SpannableString(AndroidEmoji.ensure(content));
        }
        return null;
    }


}
